package jk.spider.core.task.impl;

import jk.spider.core.SpiderController;
import jk.spider.core.task.Scheduler;
import jk.spider.core.task.threading.MonitorThread;

import org.apache.log4j.Logger;

public class SchedulerMonitor extends MonitorThread {
	protected static final Logger log = Logger.getLogger(SchedulerMonitor.class);
	protected Scheduler scheduler;

	public SchedulerMonitor(SpiderController controller, int interval, int tCount) {
		super(controller, interval, tCount);
		setDaemon(true);
		scheduler = controller.getContent().getScheduler();
	}

	@Override
	public void doMonitorTask() {
		int pctDone = 0;
		if (scheduler.getJobCount() != 0) {
			pctDone = ((scheduler.getJobDone() * 100))
					/ scheduler.getJobCount();

		}
		int spiderPctDone = 0;
		if (scheduler.getSpiderJobCount() != 0) {
			spiderPctDone = ((scheduler.getSpiderJobDone() * 100) / scheduler.getSpiderJobCount());
		}
		int thinkersPctDone = 0;
		if (scheduler.getParserJobCount() != 0) {
			thinkersPctDone = ((scheduler.getParserJobDone() * 100) / scheduler.getParserJobCount());
		}
		int extractPctDone = 0;
		if(scheduler.getExtractJobCount() != 0) {
			extractPctDone = ((scheduler.getExtractJobDone() * 100) / scheduler.getExtractJobCount());
		}
		
		log.info( "Job monitor: " + pctDone + "% [D:"+ scheduler.getDTSpiderJob() +" L:"+ scheduler.getSpiderJob()+" S:" + scheduler.getSeedJob() +"] (" + scheduler.getJobDone() + "/" + scheduler.getJobCount() + ")" +
				"  [S:" + spiderPctDone + "% (" + (scheduler.getSpiderJobCount() - scheduler.getSpiderJobDone()) + "/" + scheduler.getSpiderJobDone() + "/" + scheduler.getSpiderJobCount() + ")" +
				" | T:" + thinkersPctDone + "% (" + (scheduler.getParserJobCount() - scheduler.getParserJobDone()) + "/" + scheduler.getParserJobDone() + "/" + scheduler.getParserJobCount() + ")" +
				" | E:" + extractPctDone + "% (" + (scheduler.getExtractJobCount() - scheduler.getExtractJobDone()) + "/" + scheduler.getExtractJobDone() + "/" + scheduler.getExtractJobCount() + ")]" + 
				" | P:" + controller.getContent().getProxyQueue().size());
		
	}
}
